package com.management.controller;

import com.management.po.PersonnelForm;
import com.management.service.DeptService;
import com.management.service.PersonnelFormService;
import com.management.vo.DeptVO;
import com.management.vo.PersonnelFormVO;
import com.management.vo.UserAndCompanyVO;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author hrf
 * 人事列表控制器
 */

@Controller
@RequestMapping("/documents")
public class PersonnelFormController {

    Logger logger=Logger.getLogger(PersonnelFormController.class);

    @Autowired
    PersonnelFormService personnelFormService;

    @Autowired
    DeptService deptService;

    @RequestMapping("/personnel/personnelformlist")
    public String personnelformlist(int companyId,Model model){
        logger.info("进入了人事列表控制器");
        List<PersonnelFormVO> list=personnelFormService.findByCompanyId(companyId);
        /*List<Map> dep=new ArrayList<Map>();
        if(list!=null){
            for(PersonnelFormVO personnelFormVO : list){
                int deptid = personnelFormVO.getDeptId();
                DeptVO dept=deptService.findByDeptId(deptid);
                Map map=new HashMap();
                map.put("dept",dept);
                dep.add(map);
            }
        }*/

        model.addAttribute("list",list);
        return "personnelformlist";
    }

    @RequestMapping("/personnel/toaddpersonnelform")
    public String toaddpersonnelform(Model model){
        logger.info("进入了去增加人事页面的控制器");
        List<PersonnelFormVO> list =personnelFormService.findAll();
        model.addAttribute("list",list);
        return "addpersonnelform";
    }

    @RequestMapping("/personnel/addpersonnelform")
    public String addpersonnelform(PersonnelForm personnelForm){
        logger.info("进入了增加页面的控制器");
        boolean flag=personnelFormService.add(personnelForm);
        logger.debug("增加是否成功："+flag);
        return "redirect:personnelformlist.action";
    }

    @RequestMapping("/personnel/toupdatepersonnel")
        public String toupdatepersonnel(int personnelFormId,Model model){
        logger.debug("进入了去修改订单的页面的控制器");
        PersonnelFormVO personnelFormVO=personnelFormService.findByPersonnelFormId(personnelFormId);
        model.addAttribute("personnelFormVO",personnelFormVO);
        return "updatepersonnel";
    }

    @RequestMapping("/personnel/updatepersonnel")
    public String updatepersonnel(PersonnelForm personnelForm, HttpSession session){
        logger.info("进入了修改人事的控制器");
        logger.debug(personnelForm);
        Object obj =  session.getAttribute("userAndCompanyVO");
        UserAndCompanyVO company = (UserAndCompanyVO)obj;
        int i = company.getCompanyId();
        boolean flag=personnelFormService.update(personnelForm);
        logger.debug(flag);
        return "redirect:personnelformlist.action?companyId="+i;
    }
}
